package com.bobi.tfca.ui.mainnavigation.message.groupcreation

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MemberAPI
import com.bobi.tfca.network.api.MessageAPI
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.util.LogUtil
import com.bobi.tfca.viewmodel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GroupCreationFragmentVM: ViewModel() {

    val members by lazy {
        MutableLiveData(ArrayList<MemberResponse>())
    }


    fun createGroup(
        publicViewModel: PublicViewModel,
        groupName: String,
        choseMemberIdList: List<Int>,
        fragment: GroupCreationFragment
    ) {
        publicViewModel.apply {
            request(MessageAPI::class.java).createGroup(groupName, choseMemberIdList)
                .getResponse {
                    it.collect {
                        when (it) {
                            is ApiResponse.Error -> {
                                LogUtil.e(it.errMsg)
                            }

                            ApiResponse.Loading -> LogUtil.e("Loading")
                            is ApiResponse.Success -> {
                                withContext(Dispatchers.Main) {
                                    // 清空members
                                    members.value = ArrayList()
                                    // 添加成功，跳转回群详情
                                    fragment.requireActivity().findNavController(R.id.app_nav)
                                        .navigateUp()
                                }
                            }
                        }
                    }
                }
        }
    }

    fun getMemberById(
        publicViewModel: PublicViewModel,
        memberId: Int,
        fragment: GroupCreationFragment
    ) {
        publicViewModel.apply {
            request(MemberAPI::class.java).getMemberById(memberId).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val res = it.data
                                if (res?.code == 200) {
                                    val memberResponse = res.data!!
                                    val list = members.value
                                    list!!.add(memberResponse)
                                    members.value = list
                                } else {
                                    Toast.makeText(fragment.requireContext(), res?.msg, Toast.LENGTH_SHORT)
                                        .show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
